//
//  LeveyPopListView.m
//  LeveyPopListViewDemo
//
//  Created by Levey on 2/21/12.
//  Copyright (c) 2012 Levey. All rights reserved.
//

#import "PushMenuView.h"
#import "PushMenuViewCell.h"
#import "Utility.h"
#import "Config.h"
#import "AppDelegate.h"
#define TABLEWIDTH 228
#define TABLECELLHRIGHT 42
 static NSString *cellIdentity = @"pushMenuViewCell";


@interface PushMenuView (private)
- (void) yiqiButtonAction:(UIButton*) sender;
@end

@implementation PushMenuView

#pragma mark - initialization & cleaning up
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        self.options=[NSMutableArray array];
        UIImage *background=nil;
        float rate=UISCREEN_HEIGHT/UISCREEN_WIDTH;
        if (rate==1.5) {
            background=[Utility imageNamed:@"pushmenu_bg.png"];
        }else{
            background=[Utility imageNamed:@"pushmenu_bg_5.png"];
        }
        self.backgroundColor=[UIColor  colorWithPatternImage:background];
        UIView *nav_View=[[UIView alloc]initWithFrame:CGRectMake(0, 20, UISCREEN_WIDTH, TABLECELLHRIGHT)];
        nav_View.backgroundColor=[UIColor colorWithPatternImage:[Utility imageNamed:@"pushmenu_nav.png"]];
        [self addSubview:nav_View];
        
        [nav_View addSubview:[Utility customButton:self action:@selector(yiqiButtonAction:) normalImage:@"pushmenu_button.png" lightedImage:@"pushmenu_button.png" frame:CGRectMake(15, 1, 60, 40) title:nil tag:22]];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, frame.size.width, self.frame.size.height-64-54*2)];
        self.tableView.separatorStyle=UITableViewCellEditingStyleNone;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.tag=PUSHTABLEVIEWTAG;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.scrollEnabled=YES;
        //self.tableView.showsVerticalScrollIndicator=NO;
        self.tableView.bounces=YES;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];
        
        [self addSubview:[Utility customButton:self action:@selector(onclickPushbutton:) normalImage:@"pushmemumorebutton_normal.png" lightedImage:@"pushmemumorebutton_hover.png" frame:CGRectMake(-2, self.frame.size.height-54*2, TABLEWIDTH, 54) title:nil tag:PUSHMENUTAG]];
        
        [self addSubview:[Utility customButton:self action:@selector(onclickPushbutton:) normalImage:@"pushmenu_addbutton_normal.png" lightedImage:@"pushmenu_addbutton_hover.png" frame:CGRectMake(-2, self.frame.size.height-54, TABLEWIDTH, 54) title:nil tag:PUSHMENUTAG+1]];
        
    }
    return self;
}


-(IBAction)onclickPushbutton:(UIButton*)sender{
    [_delegate onclickPushbutton:sender.tag-PUSHMENUTAG];
}


#pragma mark - Private Methods
- (void)fadeIn{
    /*
     
     self.transform = CGAffineTransformMakeScale(1.3, 1.3);
     self.alpha = 0;
     [UIView animateWithDuration:.35 animations:^{
     self.alpha = 1;
     self.transform = CGAffineTransformMakeScale(1, 1);
     }];
     
     */
}
- (void)fadeOut{
    /*
     [UIView animateWithDuration:.35 animations:^{
     self.transform = CGAffineTransformMakeScale(1.3, 1.3);
     self.alpha = 0.0;
     } completion:^(BOOL finished) {
     if (finished) {
     [self removeFromSuperview];
     }
     }];*/
    [self removeFromSuperview];
    
}

#pragma mark - Instance Methods
- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
    [aView addSubview:self];
    if (animated) {
        [self fadeIn];
    }
}

#pragma mark - Tableview datasource & delegates


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSDictionary *dict=[self.options objectAtIndex:section];
    UIImageView *view=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, TABLEWIDTH, 42)];
    view.image=[Utility imageNamed:[dict objectForKey:@"sectionImage"]];
    return  view;
}

//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_options count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[_options objectAtIndex:section] objectForKey:@"list"] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return TABLECELLHRIGHT+2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TABLECELLHRIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    
    PushMenuViewCell *cell = (PushMenuViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity];
    if (cell ==  nil) {
        cell = [[PushMenuViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
    }
    
    if (tableView.tag==PUSHTABLEVIEWTAG) {
        NSDictionary *dict = [[[self.options objectAtIndex:indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row];
        cell.menuSubject.text=[dict objectForKey:@"str2"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(leveyPopListView:didSelectedIndex:andParam:)]) {
        NSDictionary *dict=[[[self.options objectAtIndex:indexPath.section] objectForKey:@"list"] objectAtIndex:indexPath.row];
        [self.delegate leveyPopListView:self didSelectedIndex:[indexPath row] andParam:dict];
        AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
        app.menuQzdict=[NSMutableDictionary dictionaryWithDictionary:dict];
    }
    
    // dismiss self
    //[self fadeOut];
}

- (void) yiqiButtonAction:(UIButton*) sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(leveyPopListView:didSelectedIndex:andParam:)]) {
        AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
        app.menuQzdict=nil;
        [self.delegate leveyPopListView:self didSelectedIndex:-1 andParam:nil];
    }
}
#pragma mark - TouchTouchTouch
/*
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
 // tell the delegate the cancellation
 if (self.delegate && [self.delegate respondsToSelector:@selector(leveyPopListViewDidCancel)]) {
 [self.delegate leveyPopListViewDidCancel];
 }
 
 // dismiss self
 [self fadeOut];
 }*/

-(void) dealloc{
    _delegate=nil;
}

@end
